package com.fh.module_main.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.fh.lib_base.data.bean.CityBean
import com.fh.lib_base.data.bean.CityItems
import com.fh.module_main.databinding.MainItemLayoutCitylistBinding
import com.fh.module_main.view.FastIndexView

/**
 * 城市搜索结果列表适配器
 */
class CitySearchResultListAdapter(var context: Context, var data: ArrayList<CityBean>): RecyclerView.Adapter<CitySearchResultListAdapter.BaseViewHolder>() {

    private var mDatas: List<CityBean> = data
    private lateinit var itemClickListener: ItemClickListener

    open class BaseViewHolder(val binding: ViewBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        val binding = MainItemLayoutCitylistBinding.inflate(LayoutInflater.from(context),parent,false)
        return BaseViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        var cityBean = mDatas.get(position);
        val binding = holder.binding as MainItemLayoutCitylistBinding
        binding.apply {
            mainItemCitylistName.text = cityBean.name
        }
        binding.root.setOnClickListener {
            itemClickListener.onClick(position)
        }
    }

    override fun getItemCount(): Int {
        if (mDatas != null) {
            return mDatas.size
        }
        return 0;
    }
    //回调方式 设置点击事件
    interface ItemClickListener{
        fun onClick(pos: Int)
    }
    fun setOnItemClickListener(itemClickListener: ItemClickListener){
        this.itemClickListener = itemClickListener;
    }

}